package com.xgggh.redpack;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebView;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebViewClient;
import com.xgggh.app.RxBaseActivity;
import com.xgggh.redpack.databinding.ActivityShareBinding;
import com.xgggh.redpack.databinding.ActivitySpaBinding;
import com.xgggh.view.LoadingDialog;

/**
 *  网页加载
 */
public class SpaActivity extends RxBaseActivity {
    ActivitySpaBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding= DataBindingUtil.setContentView(this,R.layout.activity_spa);
        //setSupportActionBar(mBinding.toolBarParent.toolbar);

         com.tencent.smtt.sdk.WebView x5= mBinding.contentSpa.x5;
        x5.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
        //x5.setBackgroundResource(R.color.backgroundColor);

        WebSettings webSetting = x5.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(false);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(false);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setDisplayZoomControls(false);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().sync();
            webSetting.setAppCacheEnabled(true); //LOAD_DEFAULT
            webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        x5.loadUrl("https://078hhh.com/wap/#/welcome");

        x5.setWebChromeClient(new com.tencent.smtt.sdk.WebChromeClient() {
        });

        x5.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(com.tencent.smtt.sdk.WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }

            @Override
            public void onPageFinished(com.tencent.smtt.sdk.WebView webView, String s) {
                super.onPageFinished(webView, s);
                mBinding.toolBarParent.toolbar.setVisibility(View.GONE);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(com.tencent.smtt.sdk.WebView webView, WebResourceRequest webResourceRequest, Bundle bundle) {
                return super.shouldInterceptRequest(webView, webResourceRequest, bundle);
            }

            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView webView, String s) {
                /*if ("1".equals("1")){
                        return true;
                }*/
                return super.shouldOverrideUrlLoading(webView, s);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(com.tencent.smtt.sdk.WebView webView, String s) {
                return super.shouldInterceptRequest(webView, s);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(com.tencent.smtt.sdk.WebView webView, WebResourceRequest webResourceRequest) {
                return super.shouldInterceptRequest(webView, webResourceRequest);
            }

            @Override
            public void onReceivedSslError(com.tencent.smtt.sdk.WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                super.onReceivedSslError(webView, sslErrorHandler, sslError);
            }

            @Override
            public void onReceivedHttpError(com.tencent.smtt.sdk.WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
                super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
            }

            @Override
            public void onReceivedError(com.tencent.smtt.sdk.WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
            }

        });
    }


    @Override
    public void onBackPressed() {
        if (mBinding.contentSpa.x5.canGoBack()){
            mBinding.contentSpa.x5.goBack();
        }else{
            super.onBackPressed();
        }
    }
}
